package hao.bk.searchMap;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import hao.bk.royalmap.ExtendedTextView;
import hao.bk.royalmap.GeneralAnimationUtils;
import hao.bk.royalmap.HeaderLayout;
import hao.bk.royalmap.HideKeyboardOnTouchImpl;
import hao.bk.royalmap.OnBackPressedInterface;
import hao.bk.royalmap.R;
import hao.bk.royalmap.RoyalMapActivity;
import hao.bk.royalmap.SearchBarLayout;
import hao.bk.sql.bo.Location;
import hao.bk.sql.bo.Typeshop;
import hao.bk.sql.dao.LocationDAO;
import hao.bk.sql.sqlutils.SQLiteHelper;
import hao.bk.utils.ScreenUtils;
import hao.bk.utils.Utils;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class LocalShopLayout extends LinearLayout implements 
		OnItemClickListener, TextWatcher, OnBackPressedInterface {
	
	private RoyalMapActivity main;
	/** Danh sách tất cả các location lưu trong database */
	private ArrayList<Location> locationList;
	private ListView listView;
	protected Typeshop typeshop;
	protected SearchBarLayout searchBarLayout;
	private HeaderLayout localHeader;
	private ExtendedTextView alertView;
	private SearchMapGeneralLayout generalLayout;
	
	public LocalShopLayout(Context context, Typeshop typeShop) {
		super(context);
		this.typeshop = typeShop;
		initParams();
	}
	/**
	 * Khởi tạo các tham số cho danh sách các địa điểm trong typeshop
	 */
	private void initParams() {
		main = RoyalMapActivity.getInstance();
		inflate(main, R.layout.local_shop_layout, this);
		generalLayout = main.searchMap.generalLayout;	
		localHeader = (HeaderLayout) findViewById(R.id.local_shop_header);
		listView = (ListView) findViewById(R.id.local_list_shops);
		listView.setOnItemClickListener(this);
		
		//Đối tượng thông báo
		alertView = (ExtendedTextView) findViewById(R.id.local_shop_alert);
		
		/** Vấn đề là ở đây, chưa khởi tạo DB, quá hay */
		SQLiteHelper sqLiteHelper = new SQLiteHelper(main);
		sqLiteHelper.createDataBase();
	}
	
	
	public void onItemClick(AdapterView<?> parent, View arg1, int position,
			long arg3) {
		
		// Lấy ra thông tin chi tiết của location
		searchBarLayout.hiddenKeyboard();
		Location location = (Location) parent.getItemAtPosition(position);
		if (location == null) {
			return;
		}
		//Thay đổi nội dung search
		LocalShopInformation localShopInfo = new LocalShopInformation(main, this.typeshop, location);
		GeneralAnimationUtils.startLeftToRight(this, localShopInfo);
		main.searchMap.addContent(localShopInfo);
		
	}
	
	/**
	 * cập nhật danh sách location theo typshop
	 * 
	 * @param typeshop
	 */
	public void updateShopListShow(Typeshop typeshop) {
		
		/** Vấn đề là ở đây, chưa khởi tạo DB, quá hay */
		SQLiteHelper sqLiteHelper = new SQLiteHelper(main);
		sqLiteHelper.createDataBase();
		
		this.typeshop = typeshop;
		localHeader.contentLayout.setVisibility(View.GONE);
		searchBarLayout = (SearchBarLayout) findViewById(R.id.local_shop_searchbar);
		localHeader.setHeaderTitle(main.getResources().getString(R.string.searchmap_to_local));
		listView.setOnTouchListener(new HideKeyboardOnTouchImpl(searchBarLayout.textEdt));
		// thiết lập để click vào localHeader
		main.setOnBackPressedInterface(this);

		// lấy dữ liệu location cho typeshop
		LocationDAO locationDAO = new LocationDAO(main);
		locationList = locationDAO.getLocationFromTypeshop(typeshop);

		// ẩn hiện thông báo nếu không có hãng nào
		TextView notificationNotShop = (TextView) findViewById(R.id.notify_not_shop);
		notificationNotShop.setTextSize(ScreenUtils.getBaseFontSize());
		if (locationList != null && locationList.size() > 0) {
			Collections.sort(locationList, new LocationsComparator());
			listView.setVisibility(View.VISIBLE);
			notificationNotShop.setVisibility(GONE);
		} else {
			notificationNotShop.setVisibility(VISIBLE);
			notificationNotShop.setText(main.getResources()
					.getString(R.string.Searchmap_not_shop)
					.replace("#", typeshop.getName()));
			return;
		}

		// thêm chức năng tìm kiếm theo ký tự
		searchBarLayout.textEdt.addTextChangedListener(this);
		// cập nhật thông tin header
		searchBarLayout.textEdt.setHint(main.getResources().getString(
				R.string.search_location_hit));
		searchBarLayout.textEdt.setText("");
	}


	@Override
	public void onBackPressed() {
		LocalShopLayout.this.setVisibility(View.GONE);
		main.searchMap.addContent(generalLayout);
		generalLayout.updateTypeshopList();
		generalLayout.updateActiveTab(generalLayout.TAB_SEARCH_TO, true);
		GeneralAnimationUtils.startRightToLeft(LocalShopLayout.this, generalLayout);

	}

	@Override
	public void afterTextChanged(Editable s) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void beforeTextChanged(CharSequence s, int start, int count,
			int after) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count) {
		ArrayList<Location> list = new ArrayList<Location>();
		String preSearchedText = searchBarLayout.textEdt.getText().toString();
		if (preSearchedText.length() == 0) {
			searchBarLayout.showClearBtn(false);
			list = locationList;
		} else {
			searchBarLayout.showClearBtn(true);
			list = main.getSearchedList(locationList, preSearchedText);
		}
		// Nếu tìm thấy thì hiện thị danh sách được tìm kiếm ngược lại thì hiện
		// thị thông báo
		if (list.isEmpty()) {
			listView.setVisibility(GONE);
			alertView.setVisibility(VISIBLE);
		} else {
			listView.setVisibility(VISIBLE);
			alertView.setVisibility(GONE);
			listView.setAdapter(new LocalShopAdapter(main,
					R.id.local_list_shops, list));
		}
	}
	
	public class LocationsComparator implements Comparator<Location> {
		public int compare(Location o1, Location o2) {

			int value1 = 0;
			if (o1.isFavorite() == o2.isFavorite()) {
				value1 = 0;
			} else if (o1.isFavorite() && !o2.isFavorite()) {
				value1 = -1;
			} else {
				value1 = 1;
			}
			if (value1 == 0) {
				return Utils.compareStringUTF8(o1.getName(),
						o2.getName());
			}
			return value1;
		}
	}


}
